package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.service.Gwat155Service;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwbaT;
import cn.e4j.modular.crm.entity.GwbmT;
import cn.e4j.modular.system.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by yuhuan on 2018/07/10.
 */
@RestController
@RequestMapping("api/wx/gwat155")
public class Gwat155Controller extends ControllerSupport {

    private static Logger logger = LoggerFactory.getLogger(Gwat155Controller.class);

    @Autowired
    private Gwat155Service gwat155Service;


    @RequestMapping(value = "gwat155Save")
    public ResponseData gwat155Save(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        Date date = new Date();
        GwbmT gwbm_tPO = new GwbmT();
        List<Dto> list = new ArrayList<>();
        try {

            QueryWrapper<GwbmT> wrapper = new QueryWrapper<>();
            wrapper.eq("GWBM001", currentUser.getID());
            wrapper.eq("GWBM003", LocalDate.now());
            wrapper.eq("GWBM008", inDto.getString("GWBM008"));

            GwbmT one = new GwbmT().selectOne(wrapper);
            if (one != null) {
                one.setGWBM006(inDto.getString("GWBM006"));
                one.setGWBM007(inDto.getString("GWBM007"));
                one.setGWBM009(inDto.getString("GWBM009"));
                one.update(wrapper);

                return SUCCESS;
            }

            gwbm_tPO.setGWBM001(currentUser.getID());
            //gwbm001
            //根据 人员编号+有效Y 查数据库是否存在，存在就延用已经有id, 项次+1
            Dto In = new Dto();
            In.put("GWBMENT", inDto.getString("ENT"));
            In.put("GWBM001", currentUser.getID());
            list = gwat155Service.Gwbm001check(In);
            if (list.size() == 0) {
                gwbm_tPO.setGWBM002(BigDecimal.valueOf(1));
            } else {
                gwbm_tPO.setGWBM002(BigDecimal.valueOf(list.size() + 1));
            }


            //gwbm_tPO.setGWBM003(inDto.getDate("GWBM003"));
//            gwbm_tPO.setGWBM003(CommonUtils.dateToLocalDate(inDto.getDate("GWBM003")));
            //gwbm_tPO.setGWBM003(inDto.getDate("GWBM003").toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
            gwbm_tPO.setGWBM003(inDto.getLocalDate("GWBM003"));


            gwbm_tPO.setGWBM004(inDto.getString("GWBM004"));
            gwbm_tPO.setGWBM005(inDto.getString("GWBM005"));
            gwbm_tPO.setGWBM006(inDto.getString("GWBM006"));
            gwbm_tPO.setGWBM008(inDto.getString("GWBM008"));
            gwbm_tPO.setGWBM009(inDto.getString("GWBM009"));

            gwbm_tPO.setGWBMENT(currentUser.getDBC());
            gwbm_tPO.setGWBMACTI("Y");
            gwbm_tPO.setGWBMUSER(currentUser.getID());
            gwbm_tPO.setGWBMGRUP(currentUser.getORG_ID());
            gwbm_tPO.setGWBMORIU(currentUser.getID());
            gwbm_tPO.setGWBMORIG(currentUser.getORG_ID());
            //gwbm_tPO.setGWBMDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
            gwbm_tPO.setGWBMDATE(LocalDate.now());
            gwbm_tPO.setGWBMTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            gwat155Service.gwat155Save(gwbm_tPO);

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS;
    }


    @RequestMapping(value = "getKH")
    public ResponseData getKH(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        List<Dto> list = new ArrayList<>();
        try {
            inDto.put("ID", currentUser.getID());
            inDto.put("now", LocalDate.now());
            list = gwat155Service.getKH(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        return SUCCESS(list);
    }


}
